﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            try    //觉得会出错就加try
            {
                /* 端口号 */
                string[] ports = SerialPort.GetPortNames();
                foreach (string port in ports)
                {
                    comboBox1.Items.Add(port);
                }
                comboBox1.SelectedIndex = 0;

                /* 波特率 */
                //comboBox2.Items.Add("9600");
                //comboBox2.Items.Add("115200");
                comboBox2.SelectedIndex = 0;

                /* 数据位 */
                comboBox3.SelectedIndex = 3;

                /* 停止位 */
                comboBox4.Items.Add("1");
                comboBox4.SelectedIndex = 0;

                /* 校验位 */
                comboBox5.Items.Add("无");
                comboBox5.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误：" + ex.Message, "C#串口通信");
            }

           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            /* 以下4个参数都是从窗体Form1传入的 */
            Form1.strPortName = comboBox1.Text;
            Form1.strBaudRate = comboBox2.Text;
            Form1.strDataBits = comboBox3.Text;
            Form1.strStopBits = comboBox4.Text;
            DialogResult = DialogResult.OK;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
